/*-
 * Copyright (c) 2012, Columbia University
 * All rights reserved.
 *
 * This software was developed by Georgios Kontaxis <kontaxis@cs.columbia.edu>
 * at Columbia University, New York, NY, USA, in January 2012.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Columbia University nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "cbuf.h"


struct buffer *buf_init (int nelem, int selem) {

  struct buffer *newbuffer = malloc(sizeof(struct buffer));
  if (!newbuffer) {perror("malloc"); return NULL;}

  newbuffer->buf = malloc(sizeof (char *) * nelem);
  if (!newbuffer->buf) {perror("malloc"); return NULL;}

  newbuffer->free = malloc(sizeof (int) * nelem);
  if (!newbuffer->free) {perror("malloc"); return NULL;}

  newbuffer->nelem = nelem;
  newbuffer->selem = selem;

  int i;
  for (i = 0; i < nelem; i++) {

    newbuffer->buf[i] = malloc(selem + 1);
    if (!newbuffer->buf[i]) {perror("malloc"); return NULL;}

    bzero(newbuffer->buf[i], selem + 1);

    newbuffer->free[i] = 1;
  }

  newbuffer->readp = newbuffer->buf;
  newbuffer->writep = newbuffer->buf;

  return newbuffer;
}


void buf_destroy (struct buffer *buffer) {

  int i;
  for (i = 0; i < buffer->nelem; i++)
    free (buffer->buf[i]);

  free (buffer->buf);
  free (buffer->free);
  free (buffer);
}


void buf_push1 (struct buffer *buffer, char *s) {

  strncpy(*buffer->writep, s, buffer->selem);
}


void buf_push2 (struct buffer *buffer, char *s) {

  strncpy (*buffer->writep, s, buffer->selem);
  buffer->free[buffer->writep - buffer->buf] = 0;

  buffer->writep++;
  if (buffer->writep > buffer->buf + (buffer->nelem - 1))
    buffer->writep = buffer->buf;
}


void buf_push2n (struct buffer *buffer, char *s, int n) {

  unsigned int max_n = (n<buffer->selem)?n:buffer->selem;

  strncpy (*buffer->writep, s, max_n);
  buffer->free[buffer->writep - buffer->buf] = 0;
  buffer->n = max_n;

  buffer->writep++;
  if (buffer->writep > buffer->buf + (buffer->nelem - 1))
    buffer->writep = buffer->buf;
}


char *buf_pop1 (struct buffer *buffer) {

  return *buffer->readp;
}


char *buf_pop2 (struct buffer *buffer) {

  if (buffer->free[buffer->readp - buffer->buf] == 1) 
    return NULL;

  char *tmp = *buffer->readp;
  buffer->free[buffer->readp - buffer->buf] = 1;

  buffer->readp++;
  if (buffer->readp > buffer->buf + (buffer->nelem - 1))
    buffer->readp = buffer->buf;

  return tmp;
}


char *buf_peek2 (struct buffer *buffer) {

  if (buffer->free[buffer->readp - buffer->buf] == 1) 
    return NULL;
  return *buffer->readp;
}


char *buf_peek2n (struct buffer *buffer, int *n) {

  *n = buffer->n;
  if (buffer->free[buffer->readp - buffer->buf] == 1)
    return NULL;
  return *buffer->readp;
}


#if FALSE
void buf_print (struct buffer *buffer) {
  int i;
  fprintf (stdout, "* buffer at %u\n", (unsigned int) buffer->buf);
  fprintf (stdout, "* w %u -> %u, r %u -> %u\n", 
           (unsigned int) buffer->writep, (unsigned int) *buffer->writep,
           (unsigned int) buffer->readp, (unsigned int) *buffer->readp);
  for (i = 0; i < buffer->nelem; i++)
    fprintf (stdout, "* [%d] %u -> %s\n", 
             i, (unsigned int) buffer->buf[i], buffer->buf[i]);
}
#endif


#if FALSE
int main (int argc, char *argv[]) {

  char *s;

  // 3 slots, 255 bytes
  struct buffer *mybuffer = buf_init (3, 255);

  int i;
  s = "hello";
  for (i = 0; i < 1000000; i++) {
    if (rand() % 2 == 0) {
      buf_push2 (mybuffer, s);
    } else {
      buf_peek2 (mybuffer);
      buf_pop2 (mybuffer);
    }
    buf_print (mybuffer);
  }

  buf_destroy (mybuffer);

  return 0;
}
#endif
